type platformType =
  | "AppLovin"
  | "Facebook"
  | "Google"
  | "IronSource"
  | "Liftoff"
  | "Mintegral"
  | "Moloco"
  | "Pangle"
  | "Rubeex"
  | "Tiktok"
  | "Unity";

/** 跳转类型 */
export enum InstallType {
  /** 正常跳转 */
  None = 1,
  /** 全局跳转 */
  Global = 2,
  /** 自动跳转 */
  Auto = 4,
  /** 诱导跳转 */
  Induce = 8,
}

const iosLink =
  "https://apps.apple.com/us/app/chaos-citadel-enchanted/id6744015101";
const androidLink =
  "https://play.google.com/store/apps/details?id=com.riftsky.tdmini.gb.gp";

export default class GtPlayable {
  constructor() {
    this.initChannel();
  }

  _channel: platformType = null;

  private static _instance: GtPlayable = null;
  public static get instance() {
    if (!GtPlayable._instance) GtPlayable._instance = new GtPlayable();
    return GtPlayable._instance;
  }

  initChannel() {
    //@ts-ignore
    this.channel = window.advChannels;
  }

  get channel(): platformType {
    return this._channel;
  }
  set channel(value: platformType) {
    this._channel = value;
  }

  /**
   * 安装接口
   * @param installType 安装类型
   */
  install(installType: InstallType = InstallType.None) {
    const isIOS = /(iPhone|iPad|iPod)/i.test(navigator.userAgent);

    switch (this.channel) {
      case "Facebook": 
      case "Moloco":
      {
        //@ts-ignore
        FbPlayableAd.onCTAClick();
        break;
      }
      case "Google": {
        //@ts-ignore
        ExitApi.exit();
        break;
      }
      case "Tiktok":{
        //@ts-ignore
        window.openAppStore();
        break
      }
      case "Pangle": { 
        //@ts-ignore
        playableSDK && playableSDK.openAppStore();
        break;
      }

      default: {
        //@ts-ignore
        if (typeof mraid !== "undefined") {
          // 确保 mraid 操作在 ready 状态下执行
          //@ts-ignore
          if (mraid.getState() === "loading") {
            //@ts-ignore
            mraid.addEventListener("ready", function () {
              //@ts-ignore
              mraid.open(isIOS ? iosLink : androidLink);
            });
          } else {
            //@ts-ignore
            mraid.open(isIOS ? iosLink : androidLink);
          }
        } else {
          // 降级处理
          window.location.href = isIOS ? iosLink : androidLink;
        }
        break;
      }
    }
  }

  public arrSendAction: any = {};
  /**进入场景埋点 */
  sendActionScene(name: string, sceneId?: number, description?: string) {
    if (this.arrSendAction[name]) return;
    console.log(`%c${name}`, "color:rgb(0, 186, 219); font-size: 12px;");
    this.arrSendAction[name] = true;

    let param = {
      eventName: name,
      sceneType: sceneId,
      description: description,
    };
    this.sendAction(name,param)

  }

  /**点击埋点 */
  sendActionClick(name: string, description?: string) {
    let param = {
      eventName: name,
      description: description,
    };
    this.sendAction(name,param)
  }

  /**发送埋点逻辑处理 */
  private sendAction(name: string, param: any) {
    if(this._channel == "AppLovin") return
    //@ts-ignore
    if (typeof rbiTrack !== "undefined") {
      //@ts-ignore
      rbiTrack(name, param);
    }
  }

  private isGameEnd: boolean = false;
  gameEnd() {
    if (!this.isGameEnd) {
      this.isGameEnd = true;
      this.sendActionScene("trial_end");
    }
  }

  private isGameStart: boolean = false;
  /**游戏开始的时候调用 */
  gameStart() {
    if (!this.isGameStart) {
      this.isGameStart = true;
      this.sendActionScene("page_view");
    }
  }

  private isFirstInteract: boolean = false;
  /**游戏第一次互动的时候调用 */
  firstInteract() {
    if (!this.isFirstInteract) {
      this.isFirstInteract = true;
      this.sendActionScene("first_interact");
    }
  }

  getFirstInteract() {
    return this.isFirstInteract;
  }
}

export const gtPlayable = GtPlayable.instance;
